/** \file
 *  \ingroup commandLineTools 
 *  \details This simple program takes as an argument a directorying
 *  containing DICOM images, and produces a single file as
 *  output. Single files  are preferred for our operations as they
 *  compactly contain the CT data. 
 * 
 *  Usage: ConvertDicom \<options\> where \<options\> is one or more of the following:\n
 *    \<-h\>     Display (this) usage information\n
 *    \<-i\>     Input dicom directory\n
 *    \<-o\>     Output image file name\n
 */


#ifndef DOXYGEN_SHOULD_SKIP_THIS

#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesReader.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"


typedef itk::Image< short, 3 >                ImageType;
typedef itk::GDCMImageIO                      ImageIOType;
typedef itk::GDCMSeriesFileNames              NamesGeneratorType;
typedef itk::ImageSeriesReader< ImageType >   ReaderType;
typedef itk::ImageFileWriter< ImageType >     WriterType;


void usage()
{
  std::cerr << "\n";
  std::cerr << "Usage: ConvertDicom <options> where <options> is one or more " << std::endl;
  std::cerr << "of the following:\n\n";
  std::cerr << "   <-h>     Display (this) usage information\n";
  std::cerr << "   <-i>     Input dicom directory\n";
  std::cerr << "   <-o>     Output image file name\n";


  exit(1);
}


int main( int argc, char *argv[] )
{
  bool ok;

  char* dicomDir            = new char[512];  strcpy( dicomDir, "q" );
  char* outputImageFileName = new char[512];  strcpy( outputImageFileName, "q" );

  while ( argc > 1 )
    {
    ok = false;

    if ((ok == false) && (strcmp(argv[1], "-h") == 0))
      {
      argc--; argv++;
      ok = true;
      usage();      
      }

    if ((ok == false) && (strcmp(argv[1], "-i") == 0))
      {
      argc--; argv++;
      ok = true;

      dicomDir = argv[1];

      argc--; argv++;
      }

    if ((ok == false) && (strcmp(argv[1], "-o") == 0))
      {
      argc--; argv++;
      ok = true;
      
      outputImageFileName = argv[1];

      argc--; argv++;
      }
    }

  ImageIOType::Pointer gdcmIO = ImageIOType::New();

  std::cout << "Getting file names..." << std::endl;
  NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
    namesGenerator->SetInputDirectory( dicomDir );

  const ReaderType::FileNamesContainer & filenames = namesGenerator->GetInputFileNames();

  std::cout << "Reading DICOM image..." << std::endl;
  ReaderType::Pointer dicomReader = ReaderType::New();
    dicomReader->SetImageIO( gdcmIO );
    dicomReader->SetFileNames( filenames );
  try
    {
    dicomReader->Update();
    }
  catch (itk::ExceptionObject &excp)
    {
    std::cerr << "Exception caught while reading dicom:";
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }
  
  std::cout << "Writing converted image..." << std::endl;
  WriterType::Pointer writer = WriterType::New();  
    writer->SetInput( dicomReader->GetOutput() );
    writer->UseCompressionOn();
    writer->SetFileName( outputImageFileName );
  try
    {
    writer->Update();
    }
  catch ( itk::ExceptionObject &excp )
    {
    std::cerr << "Exception caught writing imag:";
    std::cerr << excp << std::endl;
    }

  std::cout << "DONE." << std::endl;

  return 0;
}

#endif DOXYGEN_SHOULD_SKIP_THIS
